function stats_behaviour() {
    $('#select_stats_agenda').live('change', function() { 
        location.href =JSVAR_STATS_URL+'/'+$(this).val();
    });
        
    $('#select_stats_location').live('change', function() { 
        location.href =JSVAR_STATS_URL+'/'+$(this).val();
    });
}

function restore_behaviour(selectedItem) {
    alert_jqueryui_ok("Voulez-vous restaurer ce rendez-vous ? <br/><font color='red'>Cette action recharge le calendrier</font>", function() {
        post_array = {
            event_id : selectedItem.attr('id')
        };
        ajax(WEBROOT+"/getdata/restore_rdv", post_array, function(data) {
            if(getRest(data)['code'] == 200) {
                erraSuccess(getRest(data)['message']);
                selectedItem.parents('tr:first').remove();
                $('#message').dialog('close');
                location.reload();
            } else {
                erraError(getRest(data)['message']);
                $('#message').dialog('close');
            }
        });  
    });
}


function bloc_behaviour() {
    click($('.title .toggleSection'), function(e) {
        $item = $(this);
        $(this).parents('.box:first').find('.erra-form-common-inside').slideToggle(500, function() {
            if($(this).parents('.box:first').find('.erra-form-common-inside').is(':visible') == true) {
                $item.removeClass('show').addClass('hide');
            } else {
                $item.removeClass('hide').addClass('show');
            }
        });
    });
}

function time_picker_behaviour() {
    $('.jquery-timepicker-input').each(function() {
        $(this).live('change', function() {
            var slot_start =  $('input[id*="_slot_start"]');
            var slot_end =  $('input[id*="_slot_end"]');
            var event_type =  $('select[id*="_event_type"]');
       
            if(slot_start.length && slot_end.length && event_type.length) {
                var start_time = hour_str_to_timestamp(slot_start.val());
                var end_time = hour_str_to_timestamp(slot_end.val());
                if(start_time > end_time && event_type.val() != 'presta') {
                    notification_jqueryui("La fin de la plage horaire ne peut être antérieure au début !");
                    slot_start.val(slot_start.data("prev"));
                    slot_end.val(slot_end.data("prev"));
                }
                slot_start.data("prev", slot_start.val());
                slot_end.data("prev", slot_end.val());
            }
        });
    });
}

function calendar_edit_client_behaviour() {
    $('.calendar-hidden-client-id').change( function() {
        // Execute local stack before opening client informations
        executeLocalStack(function() {
            replaceHiddenId($('.client-edit'),  $('.calendar-hidden-client-id').val());
            $('.client-edit').find('form[action*="Erra_System_Action/"]').submit();
        });
    });
}




function erra_popup_behaviour() {
    each($('.erra-popup-content'), function($this) {
        $this.dialog({
            autoOpen: false
        });
    });
    
    click($( ".erra-popup" ), function($this) {
        var dialogId = $this.attr('id')+'_popup';
        var $dialog = $('#'+dialogId);
        var width=300;
        if($dialog.attr('data-width') != undefined && $dialog.attr('data-width') !== false) {
            width = $dialog.attr('data-width');
        }
        $dialog.dialog({
            'width':width
        });
        $dialog.dialog("open");
        resize_now();
        return false;
    });
    
}
 
function onclick_password_behaviour() {
    live($('#admin_mot_de_passe'), 'click', function($this) {
        $this.select();
    });
    live($('#admin_mot_de_passe_confirm'), 'click', function($this) {
        $this.select();
    });
}

function select_template_behaviour() {
    $('#select_template').change(function() { 
        location.href = $(this).val();
    });
    
    $('#default-template').click(function() {
        $('#default-template-content').slideToggle();
    });
    click($("#select-default-template"), function($this) {
        $this.select();
        // Work around Chrome's little problem
        $this.mouseup(function() {
            // Prevent further mouseup intervention
            $this.unbind("mouseup");
            return false;
        });
    });
}

function extra_field_edit_behaviour() {
    live($('select[id*="extra_field_type"]'), 'change', function($this) {
        if($this.val() == 'Erra_Fields_ComboBox' || $this.val() == 'Erra_Fields_Radio') {
            $this.parents('div.erra-field:first').find('textarea[id*="extra_field_data"]').fadeIn();
        } else {
            $this.parents('div.erra-field:first').find('textarea').fadeOut();
        }
        var newHelp = $('.extra-detail-content').find('.'+$this.val()).html();
        $this.parents('div.erra-field:first').find('.extra-details').html(newHelp)
    });
}